/**
 * 作者: sessionliang
 * 时间: 2017-08-20
 * 描述: 预约考试详情页面
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');
	var WindowTools = require('WindowTools_Core');
	//轮播图工具类
	var GallerySliderTools = require('GallerySliderTools_Core');

	//base service
	var baseService = require('Base_Service');
	var exambookingService = require('ExamBooking_Service');
	var enumHelper = require('Common_Enum');
	
	var model = null;

	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			initHomePage();
			initListeners();
		});
	}

	/**
	 * @description 监听
	 */
	function initListeners() {
		mui(".mui-content").on("tap", "#btn-help",function(){
			WindowTools.createWin(null, "exam_retry.html", {km:model.CourseType,examid:model.ExamBookingID}, {});
		});
		mui(".mui-content").on("tap","#btnCancel",function(){
			if(model!=null){
				mui.confirm("确定要取消吗？",function(e){
					if(e.index == 0){
						exambookingService.cancel(model.ExamBookingID,function(retval){
//							if(plus){
//								var list = WindowTools.getWebviewById('order_study_list');
//								list.reload();
//							}
							WindowTools.closeCurrentPage();
						});
					}
					
				});
			}
		});
	}

	/**
	 * @description 渲染頁面
	 */
	function initHomePage() {
		var id = WindowTools.getExtraDataByKey("mid");
		exambookingService.getExamBooking(id, function(retval) {
			debugger;
			model = retval;
			if(retval.IsPass != null && !retval.IsPass){
				console.log('exe');
				mui('#btn-help')[0].style.display = '';
			}
			if(retval.Status == "Waiting"){
				mui("#Result")[0].innerHTML = "您的预约申请已经提交，请耐心等待！";
			}
			else if(retval.Status == "Done"){
				mui("#Result")[0].innerHTML = "您好，您已预约成功！";
			}
			else{
				mui("#Result")[0].innerHTML = "该预约已取消！";
			}
			if(retval.CourseType == "Course2" || retval.CourseType == "Course3"){
				mui("#PlaceName")[0].parentElement.style.display = "block";
				mui("#CoachName")[0].parentElement.style.display = "block";
			}
			mui("#CoachName")[0].innerHTML = retval.CoachName;
			mui("#PlaceName")[0].innerHTML = retval.PlaceName;
			mui("#ExamTime")[0].innerHTML = getTime(retval.ExamTime);
			mui("#CourseType")[0].innerHTML = enumHelper.courseTypeFormat(retval.CourseType);
		});
	}
	
	function getTime(time){
		if(time==null){
			return 'N/A';
		}else{
			return time.replace("T",' ');
		}
	}
});